﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace FR.SAFTAnalyser.ViewModels.Workspace
{
    [Serializable]
    [XmlRoot(ElementName = "ProgramSettings", IsNullable = false)]
    public class ConfigViewModel : INotifyPropertyChanged
    {
        #region constructor
        public ConfigViewModel()
        {
            this.RememberLastOpenFiles = true;
            this.MinDate = new DateTime(2010, 01, 01);
            this.NumCasasDecimaisValidacoes = 2;
            this.FontSize = 14;
            this.ConnectionString = @"data source=;initial catalog=;timeout=200;user id=;password=;";
            this.OpenedTables = CreateDefaultOpenedTables();
        }
        #endregion constructor

        #region properties

        public string SAFTFileName { get; set; }

        public string SchemaVersion { get; set; }

        public bool RememberLastOpenFiles { get; set; }

        public string PrivateKeyFileName { get; set; }

        public string StockFileName { get; set; }

        public DateTime MinDate { get; set; }

        public int FontSize { get; set; }

        public int NumCasasDecimaisValidacoes { get; set; }

        public string PublicKeyFileName { get; set; }

        DocumentTablesDescription[] openedTables;
        public DocumentTablesDescription[] OpenedTables
        {
            get
            {
                if (openedTables == null || openedTables.Length == 0) 
                    openedTables = CreateDefaultOpenedTables();
                return openedTables;
            }
            set { if (value != null && value.Length != 0) openedTables = value; }
        }

        /// <summary>
        /// The avalonDock theme
        /// </summary>
        public int ThemeID { get; set; }

        public string ConnectionString { get; set; }

        #endregion properties

        #region methods
        #region Load Save ProgramOptions ViewModel
        /// <summary>
        /// Save program options into persistence.
        /// See <seealso cref="SaveOptions"/> to save program options on program end.
        /// </summary>
        /// <param name="settingsFileName"></param>
        /// <returns></returns>
        public static ConfigViewModel LoadOptions(string settingsFileName)
        {
            ConfigViewModel loadedViewModel = null;

            if (System.IO.File.Exists(settingsFileName))
            {
                try
                {
                    loadedViewModel = FR.SAFTAnalyser.SAFT.XmlSerializer.Deserialize<ConfigViewModel>(settingsFileName);
                }
                catch
                {
                    loadedViewModel = new ConfigViewModel();
                }
            }

            return loadedViewModel;
        }

        /// <summary>
        /// Save program options into persistence.
        /// See <seealso cref="LoadOptions"/> to load program options on program start.
        /// </summary>
        /// <param name="settingsFileName"></param>
        /// <param name="vm"></param>
        /// <returns></returns>
        public static bool SaveOptions(string settingsFileName, ConfigViewModel vm)
        {
            return FR.SAFTAnalyser.SAFT.XmlSerializer.Serializa(vm, settingsFileName);
        }
        #endregion Load Save ProgramOptions ViewModel

        internal DocumentTablesDescription[] CreateDefaultOpenedTables()
        {
            return new DocumentTablesDescription[] 
            {
                new DocumentTablesDescription { Table = DocumentTables.Customers, IsOpenByDefault = true, Description = "Clientes"},
                new DocumentTablesDescription { Table = DocumentTables.GeneralLeader, IsOpenByDefault = true, Description = "Planos contas"},
                new DocumentTablesDescription { Table = DocumentTables.GeneralLeaderEntries, IsOpenByDefault = true, Description = "Movimentos"},
                new DocumentTablesDescription { Table = DocumentTables.Header, IsOpenByDefault = true, Description = "Cabeçalho"},
                new DocumentTablesDescription { Table = DocumentTables.Invoices, IsOpenByDefault = true, Description = "Documentos comerciais"},
                new DocumentTablesDescription { Table = DocumentTables.MovementOfGoods, IsOpenByDefault = true, Description = "Mercadorias"},
                new DocumentTablesDescription { Table = DocumentTables.Payments, IsOpenByDefault = true, Description = "Recibos"},
                new DocumentTablesDescription { Table = DocumentTables.Products, IsOpenByDefault = true, Description = "Produtos"},
                new DocumentTablesDescription { Table = DocumentTables.Suppliers, IsOpenByDefault = true, Description = "Fornecedores"},
                new DocumentTablesDescription { Table = DocumentTables.Taxes, IsOpenByDefault = true, Description = "Impostos"},
                new DocumentTablesDescription { Table = DocumentTables.WorkingDocuments, IsOpenByDefault = true, Description = "Documentos de conferência"},
                new DocumentTablesDescription { Table = DocumentTables.StockHeader, IsOpenByDefault = true, Description = "Documento de existências"},
                new DocumentTablesDescription { Table = DocumentTables.StockProducts, IsOpenByDefault = true, Description = "Stocks de produtos"}
            };
        }

        #endregion methods

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
